package org.jaaksi.demo.ptr.ptr.footer;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;
import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreUIHandler;
import org.jaaksi.demo.ptr.R;

public class DefaultLoadMoreFooterView extends RelativeLayout implements LoadMoreUIHandler {

  private TextView mTextView;

  public DefaultLoadMoreFooterView(Context context) {
    this(context, null);
  }

  public DefaultLoadMoreFooterView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public DefaultLoadMoreFooterView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setupViews();
  }

  private void setupViews() {
    LayoutInflater.from(getContext()).inflate(R.layout.lib_loadmore_listview_footer, this);
    mTextView = (TextView) findViewById(R.id.tv_msg);
  }

  @Override public void onLoading(LoadMoreContainer container) {
    setVisibility(VISIBLE);
    mTextView.setText(in.srain.cube.views.loadmore.R.string.cube_views_load_more_loading);
  }

  @Override public void onLoadFinish(LoadMoreContainer container, int errorCode, boolean hasMore) {
    if (errorCode == -1) {
      mTextView.setText(in.srain.cube.views.loadmore.R.string.cube_views_load_more_error_unkonw);
    } else if (errorCode == 0) {
      if (!hasMore) {
        setVisibility(VISIBLE);
        mTextView.setText(
          in.srain.cube.views.loadmore.R.string.cube_views_load_more_loaded_no_more);
      } else {
        mTextView.setText(in.srain.cube.views.loadmore.R.string.cube_views_load_more_loading);
      }
    } else if (errorCode == 1) {
      mTextView.setText(in.srain.cube.views.loadmore.R.string.cube_views_load_more_error_net);
    } else if (errorCode == 2) {
      mTextView.setText(in.srain.cube.views.loadmore.R.string.cube_views_load_more_error_se);
    }
  }

  @Override public void onWaitToLoadMore(LoadMoreContainer container) {
    setVisibility(VISIBLE);
    mTextView.setText(
      in.srain.cube.views.loadmore.R.string.cube_views_load_more_click_to_load_more);
  }
}
